﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BIS.Portal.Core.Base;
using BIS.Portal.Core.Entity;
using BIS.Portal.Core.Global;
using BIS.Portal.Modules.CMS.BO;
using BIS.Portal.Modules.CMS.Entity;
using BIS.Utility.Converter;

namespace BIS.Portal.Modules.CMS.Adm
{
    public partial class AdminCmsArticleMediaControl : ControlBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
           
            loaddata();
        }
        private void loaddata()
        {
            #region Get data querystring

            if(Request[QueryStringKeys.ArticleId]!=null)
            {
            int iArticleId = Functions.GetInt(Request[QueryStringKeys.ArticleId]);
                int count_article = 0;
                AdminCmsArticleBO.CheckArticleId(iArticleId, ref count_article);
                if(count_article>0)
                {
                    pnlAddMedia.Visible = false;
                    btnThemMoiAddArticle.Visible = false;
                    //pnlListMedia.Visible = false;
                    //pnlButtonThemMoi.Visible = false;
                }
                else
                {
                    pnlButtonThemMoi.Visible = false;
                    pnlAddMedia.Visible = false;
                    pnlListMedia.Visible = false;

                    btnCapNhatAddArticle.Visible = false;

                }
            }
            else
            {
                pnlButtonThemMoi.Visible = false;
                pnlAddMedia.Visible = false;
                pnlListMedia.Visible = false;

                btnCapNhatAddArticle.Visible = false;
            }

            #endregion
            #region Load fill data to radioButtonList , CheckBoxList
            IList<AdminCmsCategoryEntity> list = AdminCmsCategoryBO.GetByLanguageInUseId(1);
            IList<AdminCmsCategoryEntity> listAdminCategoryEntity = new List<AdminCmsCategoryEntity>();

            AdminListViewUtility.ParseList(0, ref list, ref listAdminCategoryEntity);

            AdminListViewUtility.AddDotList(ref listAdminCategoryEntity);

            rbtlPageAliasAddArticle.DataSource = listAdminCategoryEntity;
            rbtlPageAliasAddArticle.DataValueField = "category_id";
            rbtlPageAliasAddArticle.DataTextField = "category_name";
            rbtlPageAliasAddArticle.DataBind();

            chklPageAliasAddArticle.DataSource = listAdminCategoryEntity;
            chklPageAliasAddArticle.DataValueField = "category_id";
            chklPageAliasAddArticle.DataTextField = "category_name";
            chklPageAliasAddArticle.DataBind();      
            #endregion
            #region LoadPageAlias

            Dictionary<string, FullPageSiteEntity> listOfPage = new Dictionary<string, FullPageSiteEntity>();
            PortalContext.GetPageList(0, ref listOfPage, "", false);

            ddlPageAliasAddArticle.Items.Clear();
            foreach (KeyValuePair<string, FullPageSiteEntity> page in listOfPage)
            {
                if (page.Value.PageAlias != PortalContext.PageAlias)
                {
                    ddlPageAliasAddArticle.Items.Add(new ListItem(page.Key, page.Value.PageAlias));
                }
            }
            #endregion
            #region LoadLanguage
            IList<FullSiteLanguageEntity> lstLang = PortalContext.Languages;
            ddlNgonNguAddArticle.DataSource = lstLang;
            ddlNgonNguAddArticle.DataTextField = "DisplayName";
            ddlNgonNguAddArticle.DataValueField = "LanguageInUseId";
            ddlNgonNguAddArticle.DataBind();
            #endregion

            BindDataIntoGridView();
        }
        private void GetDataForm(ref AdminCmsArticleEntity articleEntity)
        {
            articleEntity.Page_alias = ddlPageAliasAddArticle.SelectedValue;
            articleEntity.Thumb_image = txtUrlAnhDaiDien.Text;
            articleEntity.Language_in_use_id = Functions.GetInt(ddlNgonNguAddNewMedia.SelectedValue);
            articleEntity.Article_title = txtTieuDeAddArticle.Text;
            articleEntity.Article_description = txtMoTaAddArticle.Text;
            articleEntity.Article_content = "1";
            articleEntity.Post_date = Functions.GetDateTime(txtNgayDangAddArticle.Text);
            articleEntity.Is_hot_article = chkTinNoiBatAddArticle.Checked ? 1 : 0;
            articleEntity.View_count = 5;
            articleEntity.Last_date = DateTime.Now;
            articleEntity.Last_update_by =PortalContext.CurrentUser.UserName;
            articleEntity.Status = Functions.GetInt(ddlTrangThaiAddArticle.SelectedValue);
        }
        #region Session properties
        public string SearchByKeyword
        {
            get { return (null != Session["SearchByKeyword"] ? Session["SearchByKeyword"].ToString() : ""); }
            set { Session["SearchByKeyword"] = value; }
        }
        public int SearchByCategoryId
        {
            get { return (null != Session["SearchByCategoryId"] ? int.Parse(Session["SearchByCategoryId"].ToString()) : -1); }
            set { Session["SearchByCategoryId"] = value; }
        }
        public int SearchByIsHot
        {
            get { return (null != Session["SearchByIsHot"] ? int.Parse(Session["SearchByIsHot"].ToString()) : -1); }
            set { Session["SearchByIsHot"] = value; }
        }
        public int SearchByStatus
        {
            get { return (null != Session["SearchByStatus"] ? int.Parse(Session["SearchByStatus"].ToString()) : -1); }
            set { Session["SearchByStatus"] = value; }
        }
        public DateTime SearchByDateStart
        {
            get { return (null != Session["SearchByDateStart"] ? DateTime.Parse(Session["SearchByDateStart"].ToString()) : DateTime.Parse("1900-01-01")); }
            set { Session["SearchByDateStart"] = value; }
        }
        public DateTime SearchByDateEnd
        {
            get { return (null != Session["SearchByDateEnd"] ? DateTime.Parse(Session["SearchByDateEnd"].ToString()) : DateTime.Parse("9999-12-31")); }
            set { Session["SearchByDateEnd"] = value; }
        }
        #endregion

        private void BindDataIntoGridView()
        {
            int languageUseId = 0;
            try
            {
                languageUseId = PortalContext.LanguageInUseId;
            }
            catch (Exception ex)
            {

                Console.Write(ex.Message);
                languageUseId = 1;
            }
            IList<AdminCmsArticleEntity> list = AdminCmsArticleBO.SearchArticle(SearchByKeyword, SearchByCategoryId, SearchByIsHot, SearchByStatus, languageUseId, SearchByDateStart, SearchByDateEnd);
            grvListMedia.DataSource = list;
            grvListMedia.DataKeyNames = new string[] { "article_id" };
            grvListMedia.DataBind();
        }

        protected string FTPFileManagmentURL
        {
            get
            {
                try
                {
                    return "/FileManagement/AdminCateIntegratedPopupWindow.aspx";
                    //GetControlSetting<string>(ModuleSettingNames.AdminCmsArticleUpdateControl.AdminCmsArticleUpdate_ContentPage
                    //                          , PortalContext.LanguageAlias);
                }
                catch (Exception ex)
                {
                    Console.Write(ex.Message);

                    return "";
                }
            }
        }
        private void enableUpdateMediaArticle()
        {
            pnlButtonThemMoi.Visible = true;
            pnlListMedia.Visible = true;

            btnThemMoiAddArticle.Visible = false;
            btnCapNhatAddArticle.Visible = true;
        }
        private void binDataToFormUpdateArticle()
        {
            AdminCmsArticleEntity list = AdminCmsArticleBO.GetByArticleId(LastArticleId);
            if(list==null)
            {
                return;
            }
            
                if (ddlPageAliasAddArticle.Items.FindByValue(list.Page_alias) != null)
                {
                    ddlPageAliasAddArticle.SelectedValue = list.Page_alias;
                }
                if (list.Is_hot_article == 1)
                {
                    chkTinNoiBatAddArticle.Checked = true;
                }
                else
                {
                    chkTinNoiBatAddArticle.Checked = false;
                }
                ddlNgonNguAddArticle.SelectedValue = Functions.GetString(list.Language_in_use_id);
                ddlTrangThaiAddArticle.SelectedValue = Functions.GetString(list.Status);
                txtTieuDeAddArticle.Text = list.Article_title;
                txtUrlAnhDaiDien.Text = list.Thumb_image;
                txtMoTaAddArticle.Text = list.Article_description;
                txtNgayDangAddArticle.Text = Functions.GetString(list.Post_date);
        }
        private int LastArticleId
        {
            get
            {
                return Functions.GetInt(Request.QueryString[QueryStringKeys.ArticleId]);
            }
        }
        protected void btnThemMoiAddArticle_Click(object sender, EventArgs e)
        {
            //Thêm mới một Article
            AdminCmsArticleEntity articleEntity = new AdminCmsArticleEntity();
            GetDataForm(ref articleEntity);
            AdminCmsArticleBO.AddArticle(articleEntity);

            //lấy ID của bản ghi vừa insert
            int iLastArticleId=0;
            AdminCmsArticleBO.GetLastArticleId(ref iLastArticleId);
            #region Gửi queryString lên Url
		 
	
            string pageAlias =
                GetControlSetting<string>(
                    ModuleSettingNames.AdminCmsArticleControl.AsminCmsArticleMedia_LastArticleId,
                    PortalContext.LanguageAlias);

            string url = "#";
            string[] queryKeys = new string[]
                                    {
                                        QueryStringKeys.ArticleId
                                    };
            string[] queryValues = new string[]
                                     {
                                            Functions.GetString(iLastArticleId)                                     
                                     };
            if (string.IsNullOrEmpty(pageAlias))
            {
                url = WebHelpers.GetLink(UrlRewriteRules.URLListOfArticle,queryKeys,queryValues);
            }
            else
            {
                url = WebHelpers.GetLink(UrlRewriteRules.URLListOfArticle, pageAlias, queryKeys, queryValues);
            }
            #endregion

            enableUpdateMediaArticle();
            binDataToFormUpdateArticle();
        }

        protected void lbtnThemMoiMedia_Click(object sender, EventArgs e)
        {
            pnlAddMedia.Visible = true;
            pnlListMedia.Visible = true;
            pnlButtonThemMoi.Visible = true;
        }

    }
}